/*
Copyright (c) 2010 Bill Davidsen (wdavidsen@yahoo.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/************************************************************************
*	
*	MultiMenuItem specific functions
*	
*	Note: these functions require the SCS function library
*
*************************************************************************/
var debug = false;

// Begin: Interface functions
function _SCS_MM_showToolTip(source, e, text, tipClientId, tipDelay, tipDuration)
{   
	var event_ = _SCS_getEvent(e);
	var target = _SCS_getEventTarget(event_);
	
	if (target == null)
		return;
		
	var parentNode = _SCS_getParentNode(target, "TD", 3);
	
	// allow built-in image alt tag precidence over menu item tool tip
	if (target.id == source.id || (parentNode != null && parentNode.id == source.id))
	{
		if ( !(target.id) && target.tagName == "IMG")
		{
			_SCS_MM_hideToolTip(tipClientId);		
		}
		else
		{
			// opera dispays tips for anchors even if no title attribute is set
			// therefore, do not show tooltip but instead set title text to tooltip text
			if (! _isOP7)
			{
				_SCS_MM_showToolTipStep1(text, event_, tipClientId, tipDelay, tipDuration);
			}
			else
			{
				var anchor = _SCS_getChildNode(target, "A", 4);
				
				if (anchor != null)
				{
					anchor.title = text;
				}
			}	
		}
	}
}

function _SCS_MM_setCls(e, source, state, className)
{      	
	_SCS_setClass(source.id, className);
}

function _SCS_MM_setImg(e, id, imageSource)
{      		
	var image = _SCS_getObject(id);
		
	if (image != null)
		image.src = imageSource;
}

function _SCS_MM_hideMnu(menuGroup)
{
	_SCS_setVisibility(menuGroup, false);

	var backer = _SCS_getObject(menuGroup.id + "_fr");

	if (backer != null)
		backer.style.display = "none";
}

function _SCS_MM_tglMnu(source, state, voffset, hoffset)
{
	var menuGroup = _SCS_getChildNode(source, "DIV", 6);
	
	if (state)
	{   
	    var returnFlag = false;
	    	  
	    try { returnFlag = _SCS_MM_manualToggle; }
		catch (e) { /* do nothing */ }
			
		if (returnFlag) return;
						
		var top, left;
				    						
		if (menuGroup.id.indexOf("_rgrp_") > -1)
		{
		    //top = source.offsetTop - 4;
		    top = source.parentNode.offsetTop - 4;
		    //left = source.offsetLeft + source.offsetWidth + 4;
		    left = source.offsetLeft + source.offsetWidth - 3; 
		}
		else if (menuGroup.id.indexOf("_lgrp_") > -1)
		{
		    //top = source.offsetTop - 4;
		    top = source.parentNode.offsetTop - 4;
			left = source.offsetLeft - menuGroup.offsetWidth + 1; 
		}
		else 
		{
		    //top = source.offsetTop + source.offsetHeight + 2;
		    top = source.parentNode.offsetTop + source.parentNode.offsetHeight + 2;
			left = source.offsetLeft;
		}
				
		menuGroup.style.top = top + voffset + 'px';
		menuGroup.style.left = left + hoffset + 'px';  					
		menuGroup.style.visibility = "visible";
				
		_SCS_insertGroupBacker(menuGroup);		
	}
	else
	{
		_SCS_setVisibility(menuGroup, false);
		_SCS_hideBacker(menuGroup);
	}
}
		
function _SCS_MM_tglContextMnu(e)
{
	var e = _SCS_getEvent(e);
	var target = _SCS_getEventTarget(e);
	
	// this event is fired when a menu item is clicked
	// we have to return if an item from the current menu has been clicked.
	// otherwise, continue to determine what menu to display for the clicked (non menu) html element
	var targetParent = _SCS_getParentNode(target, "TD", 4);
	
	if ((targetParent != null && targetParent.id.indexOf(_SCS_MM_activeContextMenuId) == 0) 
		|| (target.id.indexOf(_SCS_MM_activeContextMenuId) == 0))
		
		return;
		
	// return when dividers are clicked	too (dividers do not have client ids assigned to them)
	if (targetParent != null && targetParent.id.length < 1)
		return;
					
	_SCS_MM_activeContextMenuId = _SCS_MM_getContextMenuId(target, _SCS_MM_contextIDs);   	
	_SCS_MM_closeContextMenus();
}

function _SCS_MM_tglContextMnuStep2(e)
{
	var e = _SCS_getEvent(e);
		
	if (_SCS_MM_activeContextMenuId != null) 
	{	
		_SCS_MM_enableContextMenu(e, _SCS_MM_activeContextMenuId);	
		return false;
	}
}

function _SCS_MM_handleTabOn(source)
{
	_SCS_MM_manualToggle = true;
	
	var menuItem = _SCS_getParentNode(source, "TD", 6);
	
	if (menuItem != null)
		_SCS_execMouseOverEvent(menuItem);
	
	_SCS_MM_manualToggle = false;
}

function _SCS_MM_processShortcutKey(menuItemId, type)
{
	var menu = _SCS_getObject(menuItemId.substring(0, menuItemId.indexOf("_")));
	_SCS_MM_execMouseouts(menu);
		
	var menuItem = _SCS_getObject(menuItemId);
	
	if (menuItem != null)
	{
		if (! _SCS_MM_isTopMenuItem(menu, menuItem))
			_SCS_MM_execMouseovers(menuItem);	
			
		_SCS_execMouseOverEvent(menuItem);			
		_SCS_MM_setMenuItemFocus(menuItem, false);
		
		if (type == "E")
			menuItem.onclick();
	}
}

function _SCS_MM_onKeyPress(e, menu)
{	
	var event_ = _SCS_getEvent(e);
	var menuItem = _SCS_getEventTarget(event_);
				
	if (menuItem.tagName == "A")
		menuItem = _SCS_getParentNode(menuItem, "TD", 4);
	
	// check to see if pressed key was any arrow, escape, or tab
	if ((event_.keyCode > 36 && event_.keyCode < 41) || event_.keyCode == 27 || event_.keyCode == 9)
	{   		
		// navigate menu via keyboard
		var isHorizMenu_ = _SCS_MM_isHorizMenu(menu);				
		var isTopMenuItem_ = _SCS_MM_isTopMenuItem(menu, menuItem);
		var menuGroup = _SCS_MM_getMenuGroup(menuItem);
		var menuType = _SCS_MM_getMenuType(menuGroup); 
		var parentMenuGroup = _SCS_MM_getParentMenuGroup(menuItem);
		var parentMenuType = _SCS_MM_getMenuType(parentMenuGroup); 			        				
				
		var cancelTab = (_isOP7) ? true : false;
		var isTab = false;
		
		//window.status = "";
		
		switch (event_.keyCode)
		{
			// tab key
			case 9:
			
				isTab = true;
							
				if (isTopMenuItem_ && isHorizMenu_)
				{
					if (! event_.shiftKey)
					{							
						if (menuGroup == null || _isFF15)
						{
							_SCS_MM_moveNext(menu, menuItem, !cancelTab, true);
//							
//							if (_isFF15)
//							    window.status = "Use arrow keys to navigate menu.";
						}
						else
						{
						    menuItem.onmouseover(); 
							_SCS_MM_moveFirst(menu, menuGroup, !cancelTab, true);							
						}
					}
					else
					{
						_SCS_MM_movePrev(menu, menuItem, !cancelTab, false);
						
						if (menuGroup != null)
							menuItem.onmouseout(); //hideMenu(menuGroup.id);
					}						
				}								
				else if (menuGroup != null)
				{
    				if (!_isFF15)
					{
					    if (! event_.shiftKey)
					    {
						    menuItem.onmouseover(); 
						    _SCS_MM_moveFirst(menu, menuGroup, !cancelTab, true);
					    }
					    else
					    {
						    menuItem.onmouseout(); //hideMenu(menuGroup.id);
						    _SCS_MM_movePrev(menu, menuItem, !cancelTab, false);
					    }
					}
//					else
//					{
//					    window.status = "Use arrow keys to navigate menu.";
//					}
				}
				else
				{
					if (! event_.shiftKey)
					{							
						if (_SCS_MM_moveNext(menu, menuItem, !cancelTab, false))
						{
							if (cancelTab)
								_SCS_MM_closeMenus_tabCancel(menu, parentMenuGroup);
							else
								_SCS_MM_closeMenus_tab(menu, parentMenuGroup);		
						}
					}
					else
					{
						_SCS_MM_movePrev(menu, menuItem, !cancelTab, true);					
					}
				}
				break;
				
			// escape key
			case 27:
				
				if (! (isTopMenuItem_ && isHorizMenu_))
					_SCS_MM_closeMenus_arrows(menu, parentMenuGroup);
					
				if (_SCS_MM_activeContextMenuId != null && isTopMenuItem_)
				{
					// close context menu and its childresn
					_SCS_MM_execMouseouts(menu);
					_SCS_setVisibility(menu, false);
				}
				
				break;
				
			// left arrow
			case 37:
				
				if (isTopMenuItem_ && isHorizMenu_)
				{
					_SCS_MM_movePrev(menu, menuItem, false, true);
					_SCS_MM_closeMenus_arrows(menu, parentMenuGroup);
				}
				else if (menuGroup != null)
				{
					if (menuType == "L")
					{
						menuItem.onmouseover(); 
						_SCS_MM_moveFirst(menu, menuGroup, false, false);
					}
					else
					{
						_SCS_MM_closeMenus_arrows(menu, parentMenuGroup);
					}
				}
				else
				{
					if (parentMenuType == "R")
						_SCS_MM_closeMenus_arrows(menu, parentMenuGroup);
				}							
				break;
			
			// up arrow	
			case 38:
						             			
				if (! (isTopMenuItem_ && isHorizMenu_))
					_SCS_MM_movePrev(menu, menuItem, false, false);
				
				break;
				
			// right arrow
			case 39:        							
				
				if (isTopMenuItem_ && isHorizMenu_)
				{					
					_SCS_MM_moveNext(menu, menuItem, false, true);
						
					if (menuGroup != null)
						_SCS_MM_closeMenus_arrows(menu, parentMenuGroup);							
				}
				else if (menuGroup != null)
				{
					if (menuType == "R")
					{
						menuItem.onmouseover(); 
						_SCS_MM_moveFirst(menu, menuGroup, false, false);
					}
					else
					{
						_SCS_MM_closeMenus_arrows(menu, parentMenuGroup);
					}							
				}
				else
				{
					if (parentMenuType == "L")
						_SCS_MM_closeMenus_arrows(menu, parentMenuGroup);
				}						
				break;
				
			// down arrow
			case 40:
						            
				 			
				if (isTopMenuItem_ && isHorizMenu_ && menuGroup != null)
				{					
					if (menuGroup.style.visibility != "visible")
						menuItem.onmouseover(); 
									
					_SCS_MM_moveFirst(menu, menuGroup, false, false);
				}
				else if (! (isTopMenuItem_ && isHorizMenu_))
				{   										
					_SCS_MM_moveNext(menu, menuItem, false, false);
				}  			
				break;

		}
		
		// to prevent bug in IE, force parent menu group to be redrawn
		if (parentMenuGroup != null)
			parentMenuGroup.className = parentMenuGroup.className; 
		
		if (cancelTab)
			return _SCS_cancelEventBubble(event_);
		
		//return;
	}
}
// tool tip timers
var timerOn = null; 
var timerOff = null;

function _SCS_MM_showToolTipStep1(text, e, tipClientId, tipDelay, tipDuration)
{   
	var obj = _SCS_getObject(tipClientId);
	var html = "<table cellspacing=0><tr><td class=ToolTip nowrap>";

	html += text + "</td></tr></table>";
	
	obj.innerHTML = html;
	obj.noWrap = true;
	
	if (timerOn) 
		clearTimeout(timerOn);
	
	var coords = _SCS_getEventCoordinates(e);
	
	timerOn = setTimeout("_SCS_MM_showToolTipStep2('" + tipClientId + "', " + coords[0] + ", " + coords[1] + ")", tipDelay);
	timerOff = setTimeout("_SCS_MM_hideToolTip('" + tipClientId + "')", tipDelay + tipDuration);
}

function _SCS_MM_showToolTipStep2(tipClientId, xcoord, ycoord)
{      
	var obj = _SCS_getObject(tipClientId);
	var rightMargin = document.body.clientWidth + document.body.scrollLeft;

	// if the toolTip runs off right
	if ((xcoord + obj.clientWidth) > rightMargin) 
		xcoord = rightMargin - obj.clientWidth - 6;

	// if toolTip runs off bottom
	if ((ycoord + obj.clientHeight) > (document.body.clientHeight + document.body.scrollTop - 20)) 
		ycoord -= obj.clientHeight + 30;

	obj.style.left = xcoord + "px";
	obj.style.top = ycoord + 15 + "px";
					
	_SCS_setVisibility(obj, true);
	_SCS_insertGroupBacker(obj);
} 

function _SCS_MM_hideToolTip(tipClientId)
{     
	if (_isOP7)
		return;
		
    if (timerOn != null) 
		clearTimeout(timerOn);
		
  	if (timerOff != null) 
		clearTimeout(timerOff);
	
	var toolTip = _SCS_getObject(tipClientId);
	
	_SCS_hideBacker(toolTip);
	_SCS_setVisibility(toolTip, false);
}

function _SCS_MM_execChildUrl(source, e)
{
	var event_ = _SCS_getEvent(e);
	var target = _SCS_getEventTarget(event_);
		
	if (target.tagName == "TD") 
	{	
		var anchor = _SCS_getChildNode(source, "A", 4);		
		_SCS_clickAnchor(anchor);
	}    	
	
	//_SCS_cancelEventBubble(event_);
}

/* Keyboard support functions  */

function _SCS_MM_closeMenus_tabCancel(menu, menuGroup)
{
	if (menuGroup == null) return;
	if (_SCS_MM_isTopMenuGroup(menu, menuGroup)) return;
	
	_SCS_MM_hideMnu(menuGroup);
	
	var parentItem = _SCS_getParentNode(menuGroup, "TD", 6);		
	var nextNodes = _SCS_MM_getNextItemNodes(menu, parentItem, "N");
	var nextItem = nextNodes[1];
				
	if (nextItem != null)
	{
		var parentAnchor = _SCS_getChildNode(parentItem, "A", 4);
											
		parentItem.onmouseout();
		_SCS_execMouseOverEvent(nextItem);
		_SCS_MM_setMenuItemFocus(nextItem, false);
	}
	else
	{	
		parentItem.onmouseout();
		var parentMenuGroup = _SCS_MM_getParentMenuGroup(parentItem);
		
		if (! _SCS_MM_isTopMenuGroup(menu, parentMenuGroup))
			_SCS_MM_closeMenus_tabCancel(menu, parentMenuGroup);
		else
			_SCS_MM_moveFirst(menu, parentMenuGroup, false, false);
	}
}

function _SCS_MM_closeMenus_tab(menu, menuGroup)
 {
	if (menuGroup == null) return;
	if (_SCS_MM_isTopMenuGroup(menu, menuGroup)) return;
	
	_SCS_MM_hideMnu(menuGroup);
	
	var parentItem = _SCS_getParentNode(menuGroup, "TD", 6);		
	var nextNodes = _SCS_MM_getNextItemNodes(menu, parentItem, "N");
	var nextItem = nextNodes[1];
				
	if (nextItem != null)
	{
		var parentAnchor = _SCS_getChildNode(parentItem, "A", 4);
		
		if (parentAnchor != null)
			parentAnchor.focus();	
										
		parentItem.onmouseout();
		_SCS_execMouseOverEvent(nextItem);
	}
	else
	{								
		parentItem.onmouseout();
		var parentMenuGroup = _SCS_MM_getParentMenuGroup(parentItem);
		_SCS_MM_closeMenus_tab(menu, parentMenuGroup);
	}
}

function _SCS_MM_closeMenus_arrows(menu, menuGroup)
{
	if (menuGroup == null) return;
	if (_SCS_MM_isTopMenuGroup(menu, menuGroup)) return;
	
	_SCS_MM_hideMnu(menuGroup);
	
	var parentItem = _SCS_getParentNode(menuGroup, "TD", 6);		
	var nextNodes = _SCS_MM_getNextItemNodes(menu, parentItem, "N");
	var nextItem = nextNodes[1];
				
	if (nextItem != null)
	{
		var parentAnchor = _SCS_getChildNode(parentItem, "A", 4);
		
		if (parentAnchor != null)
			parentAnchor.focus();	
										
		_SCS_MM_manualToggle = true;
			
		_SCS_execMouseOverEvent(parentItem);
		_SCS_MM_execMouseouts(menuGroup);
			
		_SCS_MM_manualToggle = false;			
	}
	else
	{								
		_SCS_MM_manualToggle = true;
			
		_SCS_execMouseOverEvent(parentItem);
		_SCS_MM_setMenuItemFocus(parentItem, false);
		_SCS_MM_execMouseouts(menuGroup);
			
		_SCS_MM_manualToggle = false;			
	}
}

function _SCS_MM_execMouseouts(menuGroup)
{
	if (menuGroup == null) 
		return;
		
	var cells = menuGroup.getElementsByTagName("TD");
		
	for (var i=0; i < cells.length; i++)
	{
		if (cells[i].onmouseout)
			cells[i].onmouseout();
	}
}

function _SCS_MM_getMenuType(menuGroup)
{
	var typeCode = "N";	// default to none
	
	if (menuGroup)
	{		
		if (menuGroup.id.indexOf("_rgrp_") > -1)
			typeCode = "R";
			
		else if (menuGroup.id.indexOf("_lgrp_") > -1)
			typeCode = "L";
			
		else if (menuGroup.id.indexOf("_grp_") > -1)
			typeCode = "B";
	}
				
	return typeCode;
}

function _SCS_MM_getMenuGroup(menuItem)
{
	var menuGroup = _SCS_getChildNode(menuItem, "DIV", 6);
	
	if (menuGroup)
		return menuGroup;
						
	return null;
}

function _SCS_MM_getParentMenuGroup(menuItem)
{
	var menuGroup = _SCS_getParentNode(menuItem, "DIV", 8);
	
	if (menuGroup)
		return menuGroup;
		
	return null;  
}

function _SCS_MM_getNextItemNodes(menu, menuItem, direction)
{
	var targetRow = _SCS_getParentNode(menuItem, "TR", 4);
	var targetCell = menuItem;
	var anchor = null;
	var isLast = false;
	var isInHorizGroup = _SCS_MM_isHorizMenu(menu) & _SCS_MM_isTopMenuItem(menu, menuItem);
	
	// get next menu item while skipping over dividers and disabled items		
	var ctr = 0;										
	while ((anchor == null || (anchor != null && anchor.tabIndex < 1)) && ctr < 10)
	{
		if (! isInHorizGroup)
		{
			if (direction == "P")
				targetRow = _SCS_getPrevNode(targetRow, 4, true);
			else
				targetRow = _SCS_getNextNode(targetRow, 4, true);
				
			targetCell = _SCS_getChildNode(targetRow, "TD", 4);
		}
		else
		{
			if (direction == "P")
				targetCell = _SCS_getPrevNode(targetCell, 4, true);
			else
				targetCell = _SCS_getNextNode(targetCell, 4, true);
		}					
		
		if (targetCell == null)
		{
			isLast = true;
			targetCell = null;
			anchor = null;
			break;
		}
						
		anchor = _SCS_getChildNode(targetCell, "A", 6);
		ctr++;
	}
	var nodes = new Array(targetRow, targetCell, anchor);
	return nodes;
}

function _SCS_MM_isHorizMenu(menu)
{
	var topTable = _SCS_getNestedNode(menu, "TABLE", 1);
		
	// get first row under table > tbody
	var row = _SCS_getNestedNode(topTable, "TR", 1);
	
	// get row's cells
	var cells = row.childNodes;
			
	var cellCount = _SCS_getNodeCount(cells, "TD");
						
	if (cellCount == 0)
	{
		return false;
	}			
	else if (cellCount > 1)
	{
		return true;
	}
	else 
	{
		var tbody = _SCS_getNestedNode(menu, "TBODY", 1);
		var rowCount = _SCS_getNodeCount(tbody.childNodes, "TR");
		
		if (rowCount == 1 && cellCount == 1)
		{
			// must look deeper since menu has only one row and one cell
			var menuGroup = _SCS_MM_getMenuGroup(cells[0]);
			
			// only horizontal menus have type "B" ("B" for below)
			if (_SCS_MM_getMenuType(menuGroup) == "B")
				return true;
		}
	}				
	return false;
}

function _SCS_MM_isTopMenuItem(menu, menuItem)
{
	var menuGroup = _SCS_getParentNode(menuItem, "DIV", 8);
	return _SCS_MM_isTopMenuGroup(menu, menuGroup);			
}
	
function _SCS_MM_isTopMenuGroup(menu, menuGroup)
{
	if (menuGroup && menuGroup.id == menu.id)
		return true;
	else	
		return false;
}
		
function _SCS_MM_moveFirst(menu, menuGroup, isTabEnabled, displayChildMenu)
{			
	var menuItem = _SCS_getNestedNode(menuGroup, "TD", 1);
	var anchor = _SCS_getNestedNode(menuItem, "A", 1);
	var savedToggleState = _SCS_MM_manualToggle;
	
	_SCS_MM_manualToggle = !displayChildMenu;
	
	if (anchor == null || anchor.tabIndex < 1)
	{
		menuItem = _SCS_MM_getNextItemNodes(menu, menuItem, "N")[1];	
	}
	
	if (menuItem != null)
	{
		_SCS_MM_setMenuItemFocus(menuItem, isTabEnabled);				
		_SCS_execMouseOverEvent(menuItem);
	}
	_SCS_MM_manualToggle = savedToggleState;
}

function _SCS_MM_moveLast(menu, menuGroup, isTabEnabled, displayChildMenu)
{	
	var rows = _SCS_getNestedNode(menuGroup, "TBODY", 1).childNodes;
	var savedToggleState = _SCS_MM_manualToggle;
	
	_SCS_MM_manualToggle = !displayChildMenu;
	
	if (rows != null && rows.length > 0)
	{
		var row = rows[rows.length - 1];
		var menuItem = _SCS_getNestedNode(row, "TD", 1);
		var anchor = _SCS_getNestedNode(menuItem, "A", 1);
		
		if (anchor == null || anchor.tabIndex < 1)
		{
			menuItem = _SCS_MM_getNextItemNodes(menu, menuItem, "P")[1];	
		}
		
		if (menuItem != null)
		{
			_SCS_MM_setMenuItemFocus(menuItem, isTabEnabled);				
			_SCS_execMouseOverEvent(menuItem);
		}
	}
	_SCS_MM_manualToggle = savedToggleState;
}
			
function _SCS_MM_moveNext(menu, menuItem, isTabEnabled, displayChildMenu)
{			
	var nextNodes = _SCS_MM_getNextItemNodes(menu, menuItem, "N");
	var isLast = false;
		
	_SCS_MM_manualToggle = !displayChildMenu;
	
	if (_isFF15 && isTabEnabled)
	    _SCS_MM_manualToggle = true;
		
	if (nextNodes[1] != null)
	{
		_SCS_MM_setMenuItemFocus(nextNodes[1], isTabEnabled);			    
		_SCS_execMouseOverEvent(nextNodes[1]);
	}
	else
	{
		if (!isTabEnabled)
		{
			var menuGroup = _SCS_getParentNode(menuItem, "DIV", 6);			
			_SCS_MM_moveFirst(menu, menuGroup, isTabEnabled, displayChildMenu);
		}
		isLast = true;
	}
		
	_SCS_execMouseOutEvent(menuItem);
	_SCS_MM_manualToggle = false;
	
	return isLast;
}
	
function _SCS_MM_movePrev(menu, menuItem, isTabEnabled, displayChildMenu)
{			
	var nextNodes = _SCS_MM_getNextItemNodes(menu, menuItem, "P");
	var isFirst = false;
		
	_SCS_MM_manualToggle = !displayChildMenu;
		
	if (nextNodes[1] != null)
	{
		_SCS_MM_setMenuItemFocus(nextNodes[1], isTabEnabled);					
		_SCS_execMouseOverEvent(nextNodes[1]);
	}
	else
	{
		if (!isTabEnabled)
		{
			var menuGroup = _SCS_getParentNode(menuItem, "DIV", 6);
			_SCS_MM_moveLast(menu, menuGroup, isTabEnabled, displayChildMenu);
		}
		isFirst = true;
	}
	_SCS_execMouseOutEvent(menuItem);
	_SCS_MM_manualToggle = false;
	
	return isFirst;
}
	
function _SCS_MM_execMouseovers(menuItem)
{
	var parentGroup = _SCS_MM_getParentMenuGroup(menuItem);
	
	if (parentGroup != null)
	{
		var parentItem = _SCS_getParentNode(parentGroup, "TD", 6);
		
		if (parentItem != null)
		{
			_SCS_execMouseOverEvent(parentItem);				
			_SCS_MM_execMouseovers(parentItem);
		}
	}
}

function _SCS_MM_setMenuItemFocus(menuItem, isTabEnabled)
{
	if (!isTabEnabled)
	{
		var anchor = _SCS_getNestedNode(menuItem, "A", 1);
		var menuGroup = _SCS_getParentNode(anchor, "DIV", 8);
		
		if (anchor != null && menuGroup.style.visibility == "visible")
			anchor.focus();
	}
}

// Context menu functions

function _SCS_MM_enableContextMenu(e, menuId)
{
	var menu = _SCS_getObject(menuId);	
	var scrollSpace = _SCS_getScrollSpace();				
	_SCS_setVisibility(menu, false);    	
	
	menu.style.left = e.clientX + scrollSpace[1] + "px";
	menu.style.top = e.clientY + scrollSpace[0] + "px";		
    menu.style.zIndex = 5;
    
	_SCS_setVisibility(menu, true);	
	_SCS_insertGroupBacker(menu);
	
	_SCS_MM_moveFirst(menu, menu, false, false);
}

function _SCS_MM_getContextMenuId(target, contextIdData)
{
    for (h = 0; h < contextIdData.length; h++)
    {
		var data = contextIdData[h].split(",");

		for (i = 1; i < data.length; i++)
		{
    		var pair = data[i].split("+");
		
			if (target.id == pair[0])
				return data[0];

			var parent = target.parentNode;
		
			if (parent == null)
				break;
	    
			if (parent.id == pair[0])
				return data[0];

			for (j = 1; j < pair[1]; j++)
			{	
				parent = parent.parentNode;

				if (parent == null)
					break;

				if (parent.id == pair[0])
					return data[0];	
			}
		}
	}
	return null;
}

function _SCS_MM_closeContextMenus(e)
{
	for (var i = 0; i < _SCS_MM_contextIDs.length; i++) 
	{
		var idArray = _SCS_MM_contextIDs[i].split(",");
		var menu = _SCS_getObject(idArray[0]);
				
		_SCS_MM_execMouseouts(menu);
		_SCS_setVisibility(menu, false);  		
		_SCS_hideBacker(menu);
	}
	
	if (e != null)
		_SCS_cancelEventBubble(e);
}

function _SCS_MM_discoverMenuData(searchKey)
{
	var dataArray = new Array();
	var ctr = 0;
	
    for (var i = 0; i < document.forms[0].elements.length; i++)
    {		
        var elem = document.forms[0].elements[i];
        
        if (elem.name != null)
        {
		    if (elem.name.indexOf(searchKey) > -1)
		    {
			    dataArray[ctr] = elem.value;
			    ctr++;
		    }
        }
	}
	return dataArray;
}